/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.financial.service.taxclearance;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.financial.dao.taxclearance.TaxClearanceDao;
import com.jeesite.modules.financial.dao.taxclearance.TaxClearanceDetailDao;
import com.jeesite.modules.financial.entity.taxclearance.TaxClearance;
import com.jeesite.modules.financial.entity.taxclearance.TaxClearanceDetail;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通关缴税Service
 * @author Kilo
 * @version 2021-03-10
 */
@Service
@Transactional(readOnly=true)
public class TaxClearanceService extends CrudService<TaxClearanceDao, TaxClearance> {

	@Autowired
	private TaxClearanceDetailDao taxClearanceDetailDao;
	@Autowired
	private CodeRuleService codeRuleService;
	/**
	 * 获取单条数据
	 * @param taxClearance
	 * @return
	 */
	@Override
	public TaxClearance get(TaxClearance taxClearance) {
		TaxClearance entity = super.get(taxClearance);
		if (entity != null){
			TaxClearanceDetail taxClearanceDetail = new TaxClearanceDetail(entity);
			taxClearanceDetail.setStatus(TaxClearanceDetail.STATUS_NORMAL);
			entity.setTaxClearanceDetailList(taxClearanceDetailDao.findList(taxClearanceDetail));
		}
		return entity;
	}

	/**
	 * 查询分页数据
	 * @param taxClearance 查询条件
	 * @param taxClearance.page 分页对象
	 * @return
	 */
	@Override
	public Page<TaxClearance> findPage(TaxClearance taxClearance) {
		return super.findPage(taxClearance);
	}

	/**
	 * 保存数据(批量)
	 */
	@Transactional(readOnly=false)
	public void savePl(TaxClearance taxClearance) {
		if(taxClearance.getIsNewRecord()){
			taxClearance.setTaxClearanceCode(codeRuleService.getCode("TaxClearance"));
			taxClearance.setCreateBy(UserUtils.getUser().getLoginCode());
		}
		super.save(taxClearance);
		if (taxClearance.getIsNewRecord()){
			codeRuleService.updateCurrentNumber("TaxClearance");
		}
		// 保存 TaxClearance子表
		for (TaxClearanceDetail taxClearanceDetail : taxClearance.getTaxClearanceDetailList()){
			if (!TaxClearanceDetail.STATUS_DELETE.equals(taxClearanceDetail.getStatus())){
				taxClearanceDetail.setTaxClearanceId(taxClearance);
				if (taxClearanceDetail.getIsNewRecord()){
					taxClearanceDetailDao.insert(taxClearanceDetail);
				}else{
					taxClearanceDetailDao.update(taxClearanceDetail);
				}
			}else{
				taxClearanceDetailDao.delete(taxClearanceDetail);
			}
		}
	}

	/**
	 * 保存数据（插入或更新）
	 * @param taxClearance
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(TaxClearance taxClearance) {
		if(taxClearance.getIsNewRecord()){
			taxClearance.setTaxClearanceCode(codeRuleService.getCode("TaxClearance"));
			taxClearance.setCreateBy(UserUtils.getUser().getLoginCode());
		}
		super.save(taxClearance);
		if (taxClearance.getIsNewRecord()){
			codeRuleService.updateCurrentNumber("TaxClearance");
		}
		// 保存上传图片
		FileUploadUtils.saveFileUpload(taxClearance.getId(), "taxClearance_image");
		// 保存上传附件
		FileUploadUtils.saveFileUpload(taxClearance.getId(), "taxClearance_file");
		// 保存 TaxClearance子表-->直接删除后统一插入
		taxClearanceDetailDao.deleteByTaxClearanceId(taxClearance.getTaxClearanceId());
		for (TaxClearanceDetail taxClearanceDetail : taxClearance.getTaxClearanceDetailList()){
			taxClearanceDetail.setTaxClearanceId(taxClearance);
			taxClearanceDetailDao.insert(taxClearanceDetail);
		}
	}

	/**
	 * 更新状态
	 * @param taxClearance
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(TaxClearance taxClearance) {
		super.updateStatus(taxClearance);
	}

	/**
	 * 删除数据
	 * @param taxClearance
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(TaxClearance taxClearance) {
		super.delete(taxClearance);
		TaxClearanceDetail taxClearanceDetail = new TaxClearanceDetail();
		taxClearanceDetail.setTaxClearanceId(taxClearance);
		taxClearanceDetailDao.deleteByEntity(taxClearanceDetail);
	}

}
